#!/bin/bash
#SBATCH -J HSR_254C_shi               # 指定作业的名称，方便在作业列表中识别该作业
#SBATCH -p batch                  # 指定作业要提交到的分区，不同分区可能有不同的资源配置和使用限制
#SBATCH -N 1                      # 指定作业所需的节点数量，这里设置为 1 表示只使用一个节点
#SBATCH --ntasks-per-node=1    # 每个节点的任务数
#SBATCH --cpus-per-task=10      # 每个任务的CPU核心数
#SBATCH --mem=64G              # 内存需求
#SBATCH --output=./log/254C_%j.out
#SBATCH --time=10:00:00          # 指定作业的最长运行时间，格式为 小时:分钟:秒，这里设置为
#SBATCH --no-requeue             # 作业因系统原因失败，它就不会被自动重新提交执行。
# 加载所需的软件模块

module purge
module load anaconda3
module load lib/intel2019mkl
module load mpi/openmpi-4.1.8-gcc13-ib
module load libs/netcdf/4.7.4
module load libs/hdf5/1.12.2
conda activate py311

export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK
export OMP_PLACES=cores
export OMP_PROC_BIND=close

# 打印环境信息
echo "=== CODA_HSR 作业开始 ==="
echo "时间: $(date)"
echo "作业ID: $SLURM_JOB_ID"
echo "节点: $(hostname)"
echo "MPI进程数: $SLURM_NTASKS"
echo "OMP线程数: $OMP_NUM_THREADS"
echo "MPI实现: $(which mpirun)"
echo "模块列表:"

# 根据传入的参数决定执行哪个步骤
# 如果没有提供参数，则显示帮助信息
if [ $# -eq 0 ]; then
    echo "Usage: $0 [1-9]+"
    echo "Example: $0 456 - Run steps 4, 5, and 6"
    echo "Example: $0 234 - Run steps 2, 3, and 4"
    exit 1
fi

# 遍历输入参数中的每个字符并执行对应的步骤
for step in $(echo $1 | grep -o .); do
    case $step in
        1)
            echo "Running step 1: 1.event.collect.py"

            base=P192
            python 1.event.collect.py -base $base -line 254C  -date 254C -amp 3   -CHN 3 -debug -outfile ./data/254C.HSR/events/$base.254C.H.events.h5
            python 1.event.collect.py -base $base -line 254C  -date 254C -amp 30  -CHN 2 -refh5 ./data/254C.HSR/events/$base.254C.H.events.h5 -fs 0.2 -fe 25
            python 1.event.collect.py -base $base -line 254C  -date 254C -amp 30  -CHN 1 -refh5 ./data/254C.HSR/events/$base.254C.H.events.h5 -fs 0.2 -fe 25
            python 1.event.collect.py -base $base -line 254C  -date 254C -amp 30  -CHN 0 -refh5 ./data/254C.HSR/events/$base.254C.H.events.h5 -fs 0.2 -fe 25

            ;;
        2)
            echo "Running step 2: 2.event.info.analysis.py"
            # python 2.event.info.analysis.py -base DZ155
            # python 2.event.info.analysis.py -base P192 -line 254C -amp 30 -date 254C -debug
            python 2.event.info.analysis.py -input data/254C.HSR/events/P192.254C.Z.events.h5 -debug -amp 10
            ;;
        3)
            echo "Running step 3: 3.select.py"

            cmd="python 3.select.py -date 254C -base P192 -line P250S -debug -amp 3 -input ./data/254C.HSR/events/P192.254C.H.events.h5"
            $cmd -emarker P192.LineP250S.S2N.H -S2N 
            $cmd -emarker P192.LineP250S.N2S.H -N2S 
            ;;
        4)
            echo "Running step 4: 4.corr.py"
            # 进行互相关
            cmd="python 4.corr.py -date 254C -base P402 -line P402N -debug -L 20"
            # $cmd -emarker E  -input ./data/254C.HSR/events/P192.254C.E.events.h5
            # $cmd -emarker N  -input ./data/254C.HSR/events/P192.254C.N.events.h5
            $cmd -emarker Z.N  -input ./data/254C.HSR/events/P192.254C.Z.events.h5
            # $cmd -emarker H  -input ./data/254C.HSR/events/P192.254C.H.events.h5
            ;;
        5)
            echo "Running step 5: 5.NEstack.py"
            # 叠加获取参考道
            fs=1
            fe=15
            CHN=Z
            base=P320
            line=P250S
            cmd="python 5.NEstack.py -date 254C -base $base -line $line -debug -PBH -fs $fs -fe $fe -input data/254C.HSR/events/P192.254C.$CHN.events.$base.Line$line.$CHN.T-20.20.corr.h5"

            # $cmd -emarker P340.LineP250S.$CHN.T-40.40.N2S -eventfile data/254C.HSR/events/event_select.P192.LineP250S.N2S.H.csv 
            # $cmd -emarker P340.LineP250S.$CHN.T-40.40.S2N -eventfile data/254C.HSR/events/event_select.P192.LineP250S.S2N.H.csv
            $cmd -emarker $base.Line$line.$CHN.T-40.40.ALL 
            ;;
        6)
            echo "Running step 6: 6.get.traceSeq.py"
            # 将所有数据拼接，获取trace的时间序列
            
            fs=1.0 
            fe=40.0
            base=P402
            line=P402N
            T="T100.120"
            common="mpiexec -np 12 python 6.get.traceSeq.MPI.py -mpi -date 254C -base $base -line $line -debug -PBH -fs $fs -fe $fe"
            # $cmd -emarker $base.Line$line.$CHN.T-20.20.N2S -eventfile data/254C.HSR/events/event_select.P192.LineP250S.N2S.$CHN.csv 
            # $cmd -emarker $base.Line$line.$CHN.T-20.20.S2N -eventfile data/254C.HSR/events/event_select.P192.LineP250S.S2N.$CHN.csv
            
            for CHN in Z; do
                cmd="$common -input data/254C.HSR/events/P192.254C.$CHN.events.$base.Line$line.$T.corr.h5"
                $cmd -emarker $base.Line$line.$CHN.$T.ALL
            done
            ;;
        7)
            echo "Running step 7: 7.pick.Tshift.of.NSstack.py"

            base=P264
            line=P250S
            N=50
            T="T-20.20"
            CHN=Z
            marker1="$base.Line$line.$CHN.$T.ALL"
            inputF="data/254C.HSR/traceSeq/$marker1/traces.$marker1.$line.TO0.0.F1.0.40.0.PBH.h5"
            
            fs=1.5
            fe=2.5
            python 7.pick.Tshift.of.NSstack.py -line $line -fs $fs -fe $fe -nstack $N -input $inputF

            fs=3.0
            fe=4.0
            python 7.pick.Tshift.of.NSstack.py -line $line -fs $fs -fe $fe -nstack $N -input $inputF

            fs=5.0
            fe=6.0
            python 7.pick.Tshift.of.NSstack.py -line $line -fs $fs -fe $fe -nstack $N -input $inputF

            fs=6.0
            fe=7.0
            python 7.pick.Tshift.of.NSstack.py -line $line -fs $fs -fe $fe -nstack $N -input $inputF

            fs=9.0
            fe=11.0
            python 7.pick.Tshift.of.NSstack.py -line $line -fs $fs -fe $fe -nstack $N -input $inputF

            fs=12.0
            fe=14.0
            python 7.pick.Tshift.of.NSstack.py -line $line -fs $fs -fe $fe -nstack $N -input $inputF

            ;;
        8)
            echo "Running step 8: 8.comp.tshift.py"
            
            line=P250S
            file="data/254C.HSR/traceSeq/P264.Line$line.Z.T-20.20.ALL/*.N.50.h5"
            echo $file
            python 8.merge.dtshift.py -date 254C -line $line -input "$file" -emarker T-20.20.Z.N50 -dt 1
            ;;
        9)
            echo "Running step 9: 9.freq.analysis.py"
            # 配图
            fs=1.0
            fe=3.5
            N=50
            
            for CHN in H Z N E; do
                file="data/254C.HSR/traceSeq/P264.LineP250S.$CHN.T-20.20.ALL/dt.P264.LineP250S.$CHN.T-20.20.ALL.P250S.TO0.0.F$fs.$fe.PBH.dt.P250S.N.$N.h5"
                emarker="254C.ALL.$CHN.F$fs.$fe.PBH.N$N"
                python 9.freq.analysis.py -date 254C -file $file -emarker $emarker
            done
            ;;
        A)
            echo "Running step A: 绘图"

            # CHN=E
            # F=F1.0.3.5
            # python scripts/6B.get.traceSeq.ref.py -input data/254C.HSR/traceSeq/P264.LineP250S.Z.T-20.20.ALL/traces.P264.LineP250S.Z.T-20.20.ALL.P250S.TO0.0.F1.0.40.0.PBH.h5

            # python scripts/8B.merge.dtshift.py -input data/254C.HSR/T-20.20.Z.N50.P250S.RES.001H.h5
            # # python scripts/8C.merge.dtshift.py -input data/254C.HSR/T-20.20.Z.N50.P250S.RES.001H.h5

            # for file in data/254C.HSR/traceSeq/P264.LineP250S.Z.T-20.20.ALL/dt*.h5; do
            #     python scripts/7B.dv.v.plot.py -input $file -vref 3000
            # done
            python scripts/7C.dv.vs.st.py -input data/254C.HSR/traceSeq/P264.LineP250S.Z.T-20.20.ALL/dvv.P250S.F05.0.06.0.N.50.V3000.0.h5
            python scripts/7C.dv.vs.st.py -input data/254C.HSR/traceSeq/P264.LineP250S.Z.T-20.20.ALL/dvv.P250S.F09.0.11.0.N.50.V3000.0.h5
            ;;
        *)
            echo "Invalid step: $step"
            echo "Valid steps are 1-9"
            exit 1
            ;;
    esac
done

